<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2017/3/6
 * Time: 15:30
 */

namespace Common\Model;


class MemberModel extends CommonModel
{

    const STATUS_HIDE = 0;//禁用
    const STATUS_SHOW = 1;//启用

    const AUTH_OFF = 0;//未认证
    const AUTH_ON  = 1;//已认证
    const AUTH_WAIT = 2; //待审核
    const AUTH_REFUSE = 3; //认证失败
    /**
     * 验证是否注册
     * @param $username
     * @return mixed
     */
    public function checkUserName($username) {
//        var_dump($username);//exit;
        return $this->where(['username' => $username])->count();
    }

    /**
     * 注册账户、初始化钱包
     * @param $username
     * @param $password
     * @param $parent_mid
     * @return bool
     */
    public function register($username, $password, $parent_mid) {
        if (empty($username) && empty($password)) {
            return false;
        }
        $wallet_model = new WalletModel();
        $data = array(
            'username' => $username,
            'password' => sp_password($password),
            'parent_mid' => $parent_mid,
            'create_time' => date('Y-m-d H:i:s', time()),
            'update_time' => date('Y-m-d H:i:s', time()),
        );

        $this->startTrans();
        $iscommit = true;
        $mid = $this->add($data);
        if( $mid == false) {
            $iscommit = false;
        }
        //初始化钱包
        if($wallet_model->init($mid) == false)  {
            $iscommit = false;
        }

        if($iscommit) {
            $this->commit();
            return true;
        } else {
            $this->rollback();
            return false;
        }
    }



    /**
     * 检查密码
     *
     * @param $id
     * @param $password
     *
     * @return bool
     */
    public function check_user_password($id, $password)
    {
        $pay_world = $this->where(['id' => $id])->getField('pay_password');
        return sp_compare_password($password, $pay_world);
    }
}